//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
namespace DotNetNuke.UI.WebControls
{
	public class NavDataPageHierarchyData : IHierarchyData, INavigateUIData
	{
		private DNNNode m_objNode = null;
		public NavDataPageHierarchyData(DNNNode obj)
		{
			m_objNode = obj;
		}
		public override string ToString()
		{
			return m_objNode.Text;
		}
		public virtual bool HasChildren {
			get { return m_objNode.HasNodes; }
		}
		public virtual string Path {
			get { return GetValuePath(m_objNode); }
		}
		public virtual object Item {
			get { return m_objNode; }
		}
		public virtual string Type {
			get { return "NavDataPageHierarchyData"; }
		}
		public virtual IHierarchicalEnumerable GetChildren()
		{
			NavDataPageHierarchicalEnumerable objNodes = new NavDataPageHierarchicalEnumerable();
			if (m_objNode != null) {
				foreach (DNNNode objNode in m_objNode.DNNNodes) {
					objNodes.Add(new NavDataPageHierarchyData(objNode));
				}
			}
			return objNodes;
		}
		public virtual IHierarchyData GetParent()
		{
			if (m_objNode != null) {
				return new NavDataPageHierarchyData(m_objNode.ParentNode);
			} else {
				return null;
			}
		}
		public virtual string Name {
			get { return GetSafeValue(m_objNode.Text, ""); }
		}
		public virtual string Value {
			get { return GetValuePath(m_objNode); }
		}
		public virtual string ImageUrl {
			get {
				if (String.IsNullOrEmpty(m_objNode.Image) || m_objNode.Image.StartsWith("/")) {
					return m_objNode.Image;
				} else {
					return DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().HomeDirectory + m_objNode.Image;
				}
			}
		}
		public virtual string NavigateUrl {
			get { return GetSafeValue(m_objNode.NavigateURL, ""); }
		}
		public virtual string Description {
			get { return GetSafeValue(m_objNode.ToolTip, ""); }
		}
		private string GetSafeValue(string Value, string Def)
		{
			if (Value != null) {
				return Value;
			} else {
				return Def;
			}
		}
		private string GetValuePath(DNNNode objNode)
		{
			DNNNode objParent = objNode.ParentNode;
			string strPath = GetSafeValue(objNode.Key, "");
			do {
				if (objParent == null || objParent.Level == -1)
					break;
				strPath = GetSafeValue(objParent.Key, "") + "\\" + strPath;
				objParent = objParent.ParentNode;
			} while (true);
			return strPath;
		}
	}
}
